package com.pen;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pen.shardingjdbc.ShardJdbcDemoApplication;
import com.pen.shardingjdbc.entity.Course;
import com.pen.shardingjdbc.mapper.CourseMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * 分表分库测试
 * 模型：数据库-m1，m2
 * 每个数据库分表有：d1，d2两张表
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShardJdbcDemoApplication.class)
public class ShardingJdbcDemoTest2 {
    @Autowired
    CourseMapper courseMapper;

    @Test
    public void save() {
        Course course = new Course();
        course.setUserId(11L);
        course.setCname("java 1");
        course.setCstatus("1");
        courseMapper.insert(course);

        Course course1 = new Course();
        course1.setUserId(12L);
        course1.setCname("java 1");
        course1.setCstatus("1");
        courseMapper.insert(course1);
    }


    @Test
    public void findById() {
        Course course2 = courseMapper.selectById(1398310240202219522L);
        System.out.println(course2.toString());
    }

    @Test
    public void findByUserId() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", 12);


        List<Course> courses = courseMapper.selectList(queryWrapper);
        for (Course cours : courses) {
            System.out.println(cours.toString());
        }
    }
    @Test
    public void findByUserIdAndId() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", 12);
        queryWrapper.eq("cid", 1398310240202219522L);


        List<Course> courses = courseMapper.selectList(queryWrapper);
        for (Course cours : courses) {
            System.out.println(cours.toString());
        }
    }
}
